﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.Versioning;
using System.Text;
using System.Threading.Tasks;

namespace AssemblyViewer.Common
{
    public enum EBuildMode
    {
        [Description("Unknown")]
        Unknown = 0,
        [Description("Debug")]
        Debug = 1,
        [Description("Release")]
        Release = 2
    }

    public static class AssemblyExtension
    {
        /// <summary>
        /// 获取程序集的编译模式
        /// </summary>
        /// <param name="assembly">输入的程序集</param>
        /// <returns>程序集的编译模式</returns>
        public static string GetBuildMode(this Assembly assembly)
        {
            var debugAttr = (DebuggableAttribute)assembly.GetCustomAttribute(typeof(DebuggableAttribute));
            if (debugAttr == null)
            {
                return EBuildMode.Unknown.ToString();
            }
            if (!debugAttr.IsJITOptimizerDisabled)
            {
                return EBuildMode.Release.ToString();
            }

            if (debugAttr.IsJITTrackingEnabled)
            {
                return EBuildMode.Debug.ToString();
            }

            return EBuildMode.Unknown.ToString();
        }

        /// <summary>
        /// 获取.Net Framework版本
        /// </summary>
        /// <param name="assembly">输入的程序集</param>
        /// <returns>.Net Framework版本</returns>
        public static string GetTargetFx(this Assembly assembly)
        {
            var targetFxAttr = (TargetFrameworkAttribute)assembly.GetCustomAttribute(typeof(TargetFrameworkAttribute));

            return targetFxAttr == null ? "< Fx4.0" : targetFxAttr.FrameworkDisplayName;
        }

        /// <summary>
        /// 获取目标平台
        /// </summary>
        /// <param name="assembly">输入的程序集</param>
        /// <returns>目标平台</returns>
        public static string GetProcessorArchitecture(this Assembly assembly)
        {
            AssemblyName assemblyName = assembly.GetName();

            return assemblyName.ProcessorArchitecture.ToString();
        }
    }
}
